
import OkexWebUtils from './../okex-auto-trade/common/OkexWebUtils';

const OkexCommonWebUtils = OkexWebUtils.okexCommonWebUtils();

export default class DomHelper {
    /**
     * 创建script
     * 
     * @static
     * @param {string} src js路径
     * @param {dom} [appendToDom=document.head] dom元素 ,传null则返回创建的script
     * @param {object} props script其他属性
     * @memberof DomHelper
     */
    static createScript(src, appendToDom = document.head, props) {
        const thatScript = document.createElement('script');
        thatScript.src = src;
        if (props) {
            Object.assign(thatScript, props);
        }
        if (appendToDom === null) {
            return thatScript;
        }
        appendToDom.appendChild(thatScript);
        return thatScript;
    }

    /**
     * 创建Css
     * 
     * @static
     * @param {string} href css路径
     * @param {dom} [appendToDom=document.head] dom元素,传null则返回创建的csa
     * @param {object} props css其他属性
     * @memberof DomHelper
     */
    static createCss(href, appendToDom = document.head, props) {
        const thatLink = document.createElement('link');
        thatLink.rel = 'stylesheet';
        thatLink.href = href;
        if (props) {
            Object.assign(thatLink, props);
        }
        if (appendToDom === null) {
            return thatLink;
        }
        appendToDom.appendChild(thatLink);
        return thatLink;
    }

    /**
     *替换页面元素
     *
     * @static
     * @param {selectors} oldEleSelectors 元素选择器
     * @param {element} newEle 新元素
     * @memberof DomHelper
     */
    static replaceEle(oldEleSelectors, newEle) {
        const htmlEle = document.querySelector('html');
        const oldEle = htmlEle.querySelector(oldEleSelectors);
        if (oldEle) {
            oldEle.after(newEle);
            oldEle.remove();
            document.open();
            document.write(htmlEle.innerHTML);
            document.close();
        } else {
            OkexCommonWebUtils.log(`根据oldEleSelectors："${oldEleSelectors}",未找到对应元素`);
        }
    }
}